package zhihui.mobile.ui.address;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.zgm.utils.DensityUtil;
import com.zgm.utils.UiUtils;
import com.zgm.utils.ui.UiHeaderLayout;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.address.CommunityInfo;
import zhihui.mobile.bean.address.login.Area;
import zhihui.mobile.bean.address.login.City;
import zhihui.mobile.bean.address.login.Province;
import zhihui.mobile.manager.register.RegisterManager;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.presenter.address.AddressPresenter;
import zhihui.mobile.presenter.address.IAddressContract;
import zhihui.mobile.ui.BaseActivity;
import zhihui.mobile.ui.address.adapter.AreaAdapter;

/**
 * Created by 张公铭 on 2017/7/5 0005.
 * 小区选择界面
 */

public class AddressActivity extends BaseActivity implements IAddressContract.IAddressView
{
    private IAddressContract.IAddressPresenter addressPresenter = new AddressPresenter();
    private UiHeaderLayout mHeader;
    private LinearLayout ly_area;
    private TextView tv_area;
    private EditText ed_search;
    private ListView lv_content;

    private Dialog dialog;
    private AreaAdapter provinceAdapter;
    private AreaAdapter cityAdapter;
    private AreaAdapter areaAdapter;

    private View lastProvince;
    private View lastCity;
    private ListView lv_city;
    private ListView lv_area;

    private CommunityAdapter communityAdapter;

    @Override
    public void initView()
    {
        mHeader = (UiHeaderLayout) findViewById(R.id.header_address_activity);
        mHeader.setTitle("选择小区");
        mHeader.hindRightOneButton();
        mHeader.hindRightTwoButton();
        mHeader.setLeftButtonOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                addressPresenter.clear();
                goBack(AddressActivity.class);
            }
        });

        ly_area = (LinearLayout) findViewById(R.id.ly_address_area);
        ly_area.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                addressPresenter.initArea();
            }
        });

        tv_area = (TextView) findViewById(R.id.tv_address_area);
        tv_area.setText("地区");

        ed_search = (EditText) findViewById(R.id.et_address_community);
        ed_search.setEnabled(false);
        ed_search.setOnEditorActionListener(new TextView.OnEditorActionListener()
        {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
            {
                if (actionId == EditorInfo.IME_ACTION_SEARCH)
                {
                    addressPresenter.searchCommunity(v.getText().toString());
                    return true;
                }
                return false;
            }
        });

        Drawable drawable = getResources().getDrawable(R.drawable.search);
        drawable.setBounds(0, 0, DensityUtil.dip2px(this, 16), DensityUtil.dip2px(this, 16));
        ed_search.setCompoundDrawables(drawable, null, null, null);

        lv_content = (ListView) findViewById(R.id.lv_address_content);
        communityAdapter = new CommunityAdapter(this);
        communityAdapter.setList(addressPresenter.getCurComminityList());
        lv_content.setAdapter(communityAdapter);
        lv_content.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                CommunityInfo curCommunity = (CommunityInfo) communityAdapter.getItem(position);
                RegisterManager.getInstance().getCurRegister().setCommunityId(Long.parseLong(curCommunity.getId()));
                RegisterManager.getInstance().setCurCommunityInfo(curCommunity);
                addressPresenter.clear();
                goBack(AddressActivity.class);
            }
        });
    }

    @Override
    protected int getLayoutResId()
    {
        return R.layout.address_activity;
    }

    @Override
    protected IPresenter[] getPresenters()
    {
        return new IPresenter[]{addressPresenter};
    }

    @Override
    protected void onInitPresenter()
    {
        addressPresenter.init(this, this);
    }


    @Override
    public void showAreaDialog(Province curProvince, int proPosition, City curCity, int cityPosition, Area curArea, int areaPosition)
    {
        showDialog(curProvince, proPosition, curCity, cityPosition, curArea, areaPosition);
    }

    public void showDialog(final Province curProvince, final int proPosition, City curCity, int cityPosition, Area curArea, int areaPosition)
    {
        dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(this).inflate(R.layout.address_dialog, null);

        ListView lv_province = (ListView) view.findViewById(R.id.lv_province);
        provinceAdapter = new AreaAdapter(this);
        provinceAdapter.setListener(new AreaAdapter.IAdapterLisenter()
        {
            @Override
            public void initLastView(View view)
            {
                lastProvince = view;
            }
        });

        provinceAdapter.setmList(addressPresenter.getProvinceList());
        provinceAdapter.setCurId(curProvince.getId_f());

        lv_province.setAdapter(provinceAdapter);
        lv_province.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                view.setBackgroundColor(Color.parseColor("#f5f5f5"));
                if (lastProvince != null && view != lastProvince)
                {
                    lastProvince.setBackgroundColor(Color.parseColor("#FFFFFF"));
                }
                addressPresenter.updateCity((Province) provinceAdapter.getItem(position), position);
                lastProvince = view;
            }
        });
        lv_province.setSelection(proPosition);

        lv_city = (ListView) view.findViewById(R.id.lv_city);
        cityAdapter = new AreaAdapter(this);
        cityAdapter.setmList(curProvince.getCity());
        cityAdapter.setCurId(curCity.getId());

        lv_city.setAdapter(cityAdapter);
        lv_city.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                view.setBackgroundColor(Color.parseColor("#f5f5f5"));
                if (lastCity != null && lastCity != view)
                {
                    lastCity.setBackgroundColor(Color.parseColor("#ffffff"));
                }
                addressPresenter.updateArea((City) cityAdapter.getItem(position), position);
                lastCity = view;
            }
        });
        lv_city.setSelection(cityPosition);

        lv_area = (ListView) view.findViewById(R.id.lv_area);
        areaAdapter = new AreaAdapter(this);
        areaAdapter.setmList(curCity.getArea());
        areaAdapter.setCurId(curArea.getId());
        lv_area.setAdapter(areaAdapter);
        lv_area.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                addressPresenter.getCommunityList((Area) areaAdapter.getItem(position), position);
                UiUtils.editTextFocus(ed_search);
                dialog.dismiss();
                dialog.cancel();
                dialog = null;
            }
        });
        lv_area.setSelection(areaPosition);

        //布局给Dialog
        dialog.setContentView(view);
        //获取当前Activity窗体
        Window window = dialog.getWindow();
        //设置dialog 从底部弹出
        window.setGravity(Gravity.BOTTOM);
        //获得窗体属性
        WindowManager.LayoutParams params = window.getAttributes();
        params.y = 0;
        window.setAttributes(params);
        dialog.show();
    }

    @Override
    public void refreshCity(List<City> list)
    {
        cityAdapter.setmList(list);
        cityAdapter.notifyDataSetChanged();
        lv_city.setSelection(0);
        if (lastCity != null)
        {
            lastCity.setBackgroundColor(Color.parseColor("#ffffff"));
            lastCity = null;
        }
    }

    @Override
    public void refreshArea(List<Area> list)
    {
        areaAdapter.setmList(list);
        areaAdapter.notifyDataSetChanged();
        lv_city.setSelection(0);
    }

    @Override
    public void closeDialog(String areaName)
    {
        tv_area.setText(areaName);

    }

    @Override
    public void refreshCommunityList(List<CommunityInfo> list)
    {
        communityAdapter.setList(list);
        communityAdapter.notifyDataSetChanged();
    }

    class CommunityAdapter extends BaseAdapter
    {
        private List<CommunityInfo> mList = new ArrayList<>();
        private Context mContext;
        private LayoutInflater inflater;

        public CommunityAdapter(Context context)
        {
            mContext = context;
            inflater = LayoutInflater.from(mContext);
        }

        public void setList(List<CommunityInfo> list)
        {
            if (mList == null)
            {
                mList = new ArrayList<>();
            }
            mList.clear();
            mList.addAll(list);
        }

        @Override
        public int getCount()
        {
            return mList.size();
        }

        @Override
        public Object getItem(int position)
        {
            if (mList == null)
            {
                return null;
            }
            return mList.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            TextView tv_name;
            if (convertView == null)
            {
                convertView = inflater.inflate(R.layout.address_commuinty_lite_iem, null);
                tv_name = (TextView) convertView.findViewById(R.id.tv_address_community_name);

                ViewCache viewCache = new ViewCache();
                viewCache.tv_name = tv_name;

                convertView.setTag(viewCache);
            } else
            {
                ViewCache viewCache = (ViewCache) convertView.getTag();
                tv_name = viewCache.tv_name;
            }

            CommunityInfo communityInfo = (CommunityInfo) getItem(position);
            if (communityInfo != null)
            {
                tv_name.setText(communityInfo.getName());
                tv_name.setTag(communityInfo.getId());
            }
            return convertView;
        }

        class ViewCache
        {
            public TextView tv_name;
        }
    }
}
